package com.nullpointer.playerinfo.bukkit.events;

import java.sql.SQLException;

import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;

import com.nullpointer.playerinfo.bukkit.PlayerInfoBukkit;
import com.nullpointer.playerinfo.common.Base;
import com.nullpointer.playerinfo.common.objects.Player;

public class PlayerQuit implements Listener{
    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent e) {
        Player player = Base.playerMap.get(e.getPlayer().getUniqueId());

        Bukkit.getScheduler().cancelTask(player.getPlayerTimerTaskId());
        player.setLastPlayTime(System.currentTimeMillis());


        new BukkitRunnable() {
                @Override
                public void run() {
                    try {
                        PlayerInfoBukkit.mysql.savePlayer(player);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }.runTaskAsynchronously(PlayerInfoBukkit.plugin);
        
        Base.playerMap.remove(player.getPlayerUUID());
        
    }
}
